import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import app.Propiedad;
import app.Usuario;

import database.PropiedadDAO;

/**
 * Servlet implementation class PublicarPropiedad
 */
public class PublicarPropiedad extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private PropiedadDAO DBProp = null;
	private Connection con = null;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PublicarPropiedad() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init() throws ServletException {	
		super.init();

		try {
	        // Leyendo propiedades para la BD
	        Properties prop = new Properties();
	        ClassLoader loader = this.getClass().getClassLoader ();
	        InputStream in = loader.getResourceAsStream ("database/database.properties");
	        prop.load(in);
	        in.close();
	
	        String drivers = prop.getProperty("jdbc.drivers");
	        if (drivers != null)
	            System.setProperty("jdbc.drivers", drivers);
	
	        String url = prop.getProperty("jdbc.url");
	        String user = prop.getProperty("jdbc.user");
	        String password = prop.getProperty("jdbc.password");
	
	        // Creando conexion
	        con = DriverManager.getConnection(url, user, password);
	    } catch (IOException e1) {
	       e1.printStackTrace();
	    } catch (SQLException e2) {
	    	e2.printStackTrace();
	    }
	
	    try {
	    	DBProp = new PropiedadDAO (con);
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
    }
    
    private boolean hasProblems(HttpServletRequest request) {
    	// Comprueba datos correctos y borra los incorrectos
    	return false;
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("home.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		
		HttpSession mySession = request.getSession();
		Usuario u = (Usuario)mySession.getAttribute("vuser");
		
		if (!hasProblems(request)) {
			Propiedad p = new Propiedad();
			p.setTipo(Integer.parseInt(request.getParameter("tipoPropiedad")));
			p.setDimension(Integer.parseInt(request.getParameter("dimension")));
			p.setDireccion(request.getParameter("direccion"));
			p.setOferta(request.getParameter("oferta"));

			p.setPrecio(Double.parseDouble(request.getParameter("precio")));
			p.setUsuario(u.getUsuario());
			p.setValidado(false);
			DBProp.insertar(p);
		} else {
			// Reinsertar datos
			response.sendRedirect("publish.jsp");
		}

		out.println("" +
			"<head>" +
			"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/regStyle.css\">" +
			"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" +
      		"<title>Publicado con exito</title>" +
      		"</head>" +
      		"<body>" +
      		"	<table class=\"mainTable\">" +
            "		<tr>" +
            "	   		<td>" +
            "      			<img width=\"100px\" height=\"50px\" src=\"img/logo.jpg\" />" +
            "   		</td>" +
            "		</tr>" +
            "		<tr> <td><hr color=\"#ffa200\" width=\"100%\" size=\"3px\"/></td> </tr>" +
            "		<tr>" +
            "			<th colspan=\"1\" class=\"centerButton\">" +
            "				Su propiedad ha sido publicada con exito." +
            "			</th> " +
			"   	</tr>" +
            "		<tr>" +
            "			<th colspan=\"1\" class=\"centerButton\"> " +
            "           	<input type=\"button\" value=\"Aceptar\" onclick=\"window.location='home.jsp'\"/>" +
            "			</th>" +
			"   	</tr>" +
			"	</table>" +
      		"</body>"
	    );
		
		
	}
}
